package de.jkoslows.decisions.control;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import de.jkoslows.decisions.entity.ExpressionParameter;
import de.jkoslows.decisions.entity.Item;
import de.jkoslows.decisions.entity.Parameter;
import static de.jkoslows.util.control.Util.*;

public class Decision {

	HashSet<Item> items;
	HashSet<ExpressionParameter> parameters;
	private boolean containsAllParameters = true;

	public Decision() {
		this.items = new HashSet<Item>();
		this.parameters = new HashSet<ExpressionParameter>();
	}
	
	public Decision(boolean containsAllParameters){
		this.containsAllParameters = containsAllParameters;
	}

	public void addItem(Item item) {
		items.add(assertNotNull(item, Item.class));
	}

	public void addItems(Collection<Item> items) {
		assertNotNull(items, Collection.class);
		for (Item item : items) {
			addItem(item);
		}
	}

	public void addParameter(ExpressionParameter parameter) {
		parameters.add(assertNotNull(parameter, ExpressionParameter.class));

	}

	public Collection<Item> decide() {
		Set<Item> filteredItems = filter(items, parameters);
		return filteredItems;

	}

	private Set<Item> filter(HashSet<Item> items, HashSet<ExpressionParameter> parameters) {
		Set<Item> positiveList = new HashSet<Item>(); 
		Set<Item> negativeList = new HashSet<Item>();
		for (Parameter parameter : parameters) {
			for (Item item : items) {
				if(item.hasParameter(parameter))
					positiveList.add(item);
				else
					negativeList.add(item);
					
					
			}
		}
		
		if(containsAllParameters)
			return removeItemsWhereParametersAreMissing(positiveList, negativeList);
		else
			return positiveList;
	}

	private Set<Item> removeItemsWhereParametersAreMissing(
			Set<Item> positiveList, Set<Item> negativeList) {
		boolean removeAll = positiveList.removeAll(negativeList);
		return positiveList;
	}

	public void addParameters(Collection<ExpressionParameter> paramters) {
		assertNotNull(items, Collection.class);
		for (ExpressionParameter expressionParameter : paramters) {
			addParameter(expressionParameter);
		}
		
	}

}
